package com.ujiuye.demo;

import java.sql.*;

public class MyCatTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1、注册数据库的驱动
//        Driver driver=new com.mysql.jdbc.Driver();
//        DriverManager.registerDriver(driver);
        String driver = "com.mysql.jdbc.Driver";
        Class.forName(driver);//java基础反射

        // 2.获取数据库连接

        String url = "jdbc:mysql://192.168.21.129:8066/TESTDB";
        Connection connection = DriverManager.getConnection(url, "mycat118", "123");

        //3.获取操作数据库的对象
        Statement statement = connection.createStatement();
        String sql = "select * from users";
        ResultSet resultSet = statement.executeQuery(sql);

        //4.遍历结果集，取出数据
        while (resultSet.next()) {
            String name = resultSet.getString("remarks");
            System.out.println("名字：" + name);
        }


        /*//更新数据
        String sql2 = "UPDATE users set remarks='小萌' where id=1001";
        int resule = statement.executeUpdate(sql2);

        System.out.println(resule);

        //插入数据
        for (int i = 1; i < 10; i++) {
            String sql3 = "insert into users(id,remarks) values(102" + i + ",'小黑"+i+"')";
            int result3 = statement.executeUpdate(sql3);
            System.out.println(result3);
        }*/


        statement.close();
        connection.close();

    }
}
